﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using IYAKASAPortal.Services;
using BLL;
using Model;
using DBUtility;
using System.Collections.Generic;

namespace IYAKASAPortal
{
    public partial class Frame_Left : System.Web.UI.Page
    {

        public string g_strTreeJS = "";
        private UserInfo userInfo { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
            {
                string userName = Session["UserName"].ToString();
                userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
            }
            if (userInfo != null)
            {
                string userRole = userInfo.UserRole;
                string[] roles = userRole.Split(',');
                string result = "";
                string rights = ",";
                for (int i = 0; i < roles.Length; i++)
                {
                    if (!string.IsNullOrEmpty(roles[i]))
                    {
                        result += DbHelper.GetValue("select rights from SYSTEM_ROLE_RIGHT where role='" + roles[i] + "'");
                    }
                }
                string[] rightsArr = result.Split(',');
                for (int i = 0; i < rightsArr.Length; i++)
                {
                    if (!string.IsNullOrEmpty(rightsArr[i]))
                    {
                        if (rights.IndexOf("," + rightsArr[i] + ",") < 0)
                        {
                            rights += rightsArr[i] + ",";
                        }
                    }
                }

                string[] rightsSrot = rights.Substring(1, rights.Length - 2).Split(',');
                Array.Sort(rightsSrot);
                string newRights = ",";
                for (int i = 0; i < rightsSrot.Length; i++)
                {
                    newRights += rightsSrot[i] + ",";
                }

                SYSTEM_MODULE_BLL m_Moudle_BLL = new SYSTEM_MODULE_BLL();
                SYSTEM_MODULE m_Moudle = new SYSTEM_MODULE();
                List<SYSTEM_MODULE> lstMoudle = new List<SYSTEM_MODULE>();
                int id = 0;

                string[] rightsList = newRights.Split(',');
                for (int i = 0; i < rightsList.Length; i++)
                {
                    if (!string.IsNullOrEmpty(rightsList[i]))
                    {
                        id = int.Parse(rightsList[i]);
                        m_Moudle = m_Moudle_BLL.GetModel(id);
                        if (m_Moudle.morder.HasValue)
                        {
                            lstMoudle.Add(m_Moudle);
                        }

                    }
                }

                lstMoudle.Sort((a, b) => a.morder.Value.CompareTo(b.morder.Value));

                foreach (var v in lstMoudle)
                {
                    if (string.IsNullOrEmpty(v.murl))
                    {
                        g_strTreeJS += "d.add(" + v.mid + "," + v.pid + ",'" + v.mname + "','');";
                    }
                    else
                    {
                        if (v.murl.Contains("?"))
                        {
                            g_strTreeJS += "d.add(" + v.mid + "," + v.pid + ",'" + v.mname + "','" + v.murl + "&random=" + new Random().NextDouble().ToString() + "');";
                        }
                        else
                        {
                            g_strTreeJS += "d.add(" + v.mid + "," + v.pid + ",'" + v.mname + "','" + v.murl + "?random=" + new Random().NextDouble().ToString() + "');";
                        }
                    }
                }
            }

        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="conn">连接符</param>
        /// <param name="user">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="popedom">权限</param>
        /// <param name="errmsg">登录错误消息</param>
        /// <returns>返回登录是否成功</returns>
        public static bool Login(string conn, string user, string password, out uint popedom, out string errmsg)
        {
            errmsg = "";
            popedom = 0;
            return false;
        }
    }
}
